package Controller;

import Bean.FileBean;
import Dao.FileSqlOperation;
import Dao.Impl.FileSqlOperationImpl;
import Utils.FileOperation;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;

@WebServlet("/select")
public class SelectServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
        System.out.println("查询请求收到");
        FileSqlOperation fileSqlOperation=new FileSqlOperationImpl(new FileOperation());
        List<FileBean> list=fileSqlOperation.selectFile();
/*        json字符串格式传输
        request.setAttribute("FileBean", JSON.toJSONString(list));
        request.getRequestDispatcher("index.jsp").forward(request,response);*/
        String jsonStirng =JSON.toJSONString(list);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonStirng);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    doGet(request,response);
    }
}
